<?php
/**
 * The Items model class for abstracting operations to the Items Entity
 *
 */

class Items extends Zend_Db_Table
{
    protected $_name = 'items'; // The name of the database entity
    protected $_primary = 'id'; // The primary key field for this database entity

    // Other database entities that are dependant on this entity
    protected $_dependentTables = array('Status', 'Images');

    protected $_referenceMap    = array(
        'User' => array(
            'columns'           => 'uid',
            'refTableClass'     => 'Users',
            'refColumns'        => 'uid'
        )
    );


    /**
     * READ ONLY
     *
     */
	
    public function getItems($uid)
    {
        /*
         * Create a Zen_Db_Table_Select and set the integrity check to false
         * so that Joins can be made with other tables. These CANNOT be modified
         */
        $select = $this->select()->setIntegrityCheck(false);
        $select->from('items')
               ->join('status', 'items.id = status.id')
               ->order('status.created DESC')
               ->where('items.uid = ?', $uid);
        $result = $this->fetchAll($select)->toArray();
		
		// Format the prices correctly
		return $result;
    }
	/*

        $select = $this->select()->setIntegrityCheck(false);
        $select->from(array('i' => 'items'), array('id', 'title', 'description', 'cid'))
               ->join(array('s' =>'status'), 'i.id = s.id')
               ->order('s.created DESC')
               ->where('i.uid = ?', $uid);
        return $this->fetchAll($select)->toArray();
    }
	*/
    public function getBasicItem($id)
    {
        $session = new Zend_Session_Namespace('User');
        $user = $session->user;
        return $this->fetchRow($this->select()->where('id = ?', $id)->where('uid = ?', $user->uid));
    }

    /**
     * READ ONLY
     *
     */
    public function getItem($id, $uid = null)
    {
        /*
	         * Create a Zen_Db_Table_Select and set the integrity check to false
	         * so that Joins can be made with other tables. These CANNOT be modified
	         */
			 
		$session = new Zend_Session_Namespace('User');
        $user = $session->user;
		
        $select = $this->select()->setIntegrityCheck(false);
        $select->from('items')
               ->join('status', 'items.id = status.id')
               ->where('items.id = ?', $id)
			   ->where('items.uid = ?', $user->uid);
			   
	    $row = $this->fetchRow($select);
		
		if($row)
		{
			$row = $row->toArray();
		}
		
        return $row;

    }
}